import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

import '../../DouBao/MessageModel.dart';


class DBManager{
  final int _version = 1;//版本号
  final String _databaseName = 'fuck.db';//数据库名称

  ///收藏表
  static const String messageTable = 'message';//表名
  static const String id = 'id';//主键
  static const String messageContent = 'messageContent';//文本内容
  static const String imageUrlList = 'imageUrlList';//图片内容
  static const String userType = 'userType'; //用户类型



  static DBManager? _instance;
  static DBManager getInstance() => _instance ??= DBManager();

  static Database? _database;
  Future<Database> get getDatabase async => _database ??= await _initSQl();

  ///初始化数据库
  Future<Database> _initSQl() async{
    var dbPath = await getDatabasesPath();
    var path = join(dbPath,_databaseName);
    return await openDatabase(path,version: _version,onCreate: _onCreate);
  }

  ///创建表
  Future _onCreate(Database db,int version) async{
    String favoriteSQL =
    '''CREATE TABLE $messageTable(
      $id INTEGER PRIMARY KEY,
      $messageContent TEXT,
      $imageUrlList TEXT,
      $userType INTEGER
      )
      ''';

    await db.execute(favoriteSQL);
    await db.insert(messageTable,MessageModel(
      id: 0,
      messageContent: '嗨，你好，我是你的新朋友豆包！初次见面很开心。我呢，可以回答你的各种问题，给你工作学习上提供帮助，还能随时陪你聊天。嗯，你想问点什么呢？',
      imageUrlList: [],
      userType: 0,
    ).toJson());
  }
}
